package com.jagochan.module.business.controller;


import com.jagochan.common.result.PageVO;
import com.jagochan.common.result.ResultUtils;
import com.jagochan.common.result.ResultVo;
import com.jagochan.module.business.pojo.po.Article;
import com.jagochan.module.business.pojo.vo.ArticleVO;
import com.jagochan.module.business.service.ArticleService;
import com.jagochan.module.business.service.TopicService;
import com.jagochan.module.business.service.UserService;
import com.jagochan.module.rbac.common.exception.BusinessException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.apache.ibatis.annotations.Delete;
import org.hibernate.validator.constraints.Range;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Validated
@Tag(name = "文章(帖子)")
@RestController
@RequestMapping("/community/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @Operation(summary = "根据话题ID分页查询帖子列表")
    @GetMapping("/page")
    public ResultVo<PageVO<ArticleVO>> page(@Parameter(description = "话题ID", example = "2") @NotNull(message = "话题ID不能为空") Long topicId,
                                            @Parameter(description = "分页参数（页码）", example = "1") @Range(min = 1L, max = 100L, message = "页码不能小于1或大于100")
                                            int pageNo) {
        PageVO<ArticleVO> pageVO = articleService.pageArticle(topicId, pageNo);
        return ResultUtils.success("查询成功", pageVO);
    }

    @Operation(summary = "根据帖子(文章)ID查询帖子(文章)详情")
    @GetMapping("/get")
    public ResultVo<ArticleVO> get(@Parameter(description = "帖子(文章)ID", example = "2") @NotNull(message = "帖子(文章)ID不能为空") Long id) {
        ArticleVO vo = articleService.getArticleDetail(id);
        return ResultUtils.success("查询成功", vo);
    }

    @Operation(summary = "根据ID删除帖子")
    @DeleteMapping("/delete")
    public ResultVo<Boolean> delete(@Parameter(description = "帖子ID", example = "666") @NotNull(message = "帖子ID不能为空") Long id) {
        Article article = articleService.getById(id);
        if (article == null) {
            return BusinessException.raise("ID为【" + id + "】的帖子不存在");
        }

        if (articleService.removeById(id)) {
            return ResultUtils.success("删除成功", true);
        }
        return ResultUtils.error("删除失败", false);
    }

}
